package com.googlecode.cnasort.cc.rmi.common;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.Writer;

/**
 * class to locate our most "secure" policy file
 */
public class PolicyFileLocator
{
    public static final String POLICY_FILE_NAME = "/allow_all.policy";

    public static String getLocationOfPolicyFile()
    {
        try {
            File tempFile = File.createTempFile("rmi-base", ".policy");

            try( InputStream is = PolicyFileLocator.class.getResourceAsStream( POLICY_FILE_NAME ) ) {
                try( Writer writer = new BufferedWriter(new FileWriter(tempFile)) ) {
                    int read = 0;

                    while((read = is.read()) != -1) {
                        writer.write(read);
                        }
                    }
                }

            tempFile.deleteOnExit();

            return tempFile.getAbsolutePath();
            }
        catch( IOException e ) {
            throw new RuntimeException(e);
            }
    }
}
